﻿package Cain
{
	import flash.display.DisplayObject;
	import flash.display.Stage;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.external.ExternalInterface;
	import flash.net.navigateToURL;
	/**
	 * ...
	 * @author Cain@2009 in ADCAST
	 * 自動全景 v1.0
	 * 傳入九宮格陣列 
	 * 1 2 3
	 * 4 5 6
	 * 7 8 9
	 * 陣列名如下方宣告
	 * 視窗變化傳回"resize"事件
	 * 另有 width 及 height 可讀
	 */
	public class AutoFull extends EventDispatcher
	{
		private var bg_1_arr:Array = new Array();
		private var bg_2_arr:Array = new Array();
		private var bg_3_arr:Array = new Array();
		private var bg_4_arr:Array = new Array();
		private var bg_5_arr:Array = new Array();
		private var bg_6_arr:Array = new Array();
		private var bg_7_arr:Array = new Array();
		private var bg_8_arr:Array = new Array();
		private var bg_9_arr:Array = new Array();
		private var mc_1_arr:Array = new Array();
		private var mc_2_arr:Array = new Array();
		private var mc_3_arr:Array = new Array();
		private var mc_4_arr:Array = new Array();
		private var mc_5_arr:Array = new Array();
		private var mc_6_arr:Array = new Array();
		private var mc_7_arr:Array = new Array();
		private var mc_8_arr:Array = new Array();
		private var mc_9_arr:Array = new Array();
		private var stg:Stage;
		private var ww:Number;
		private var hh:Number;
		private var min_hh:Number;
		private var min_ww:Number;
		public function AutoFull(mc:DisplayObject,obj:Object,_min_ww:Number = 0,_min_hh:Number=0)
		{
			stg = mc.stage;
			set_bg_arr(obj);
			set_mc_arr(obj);
			min_ww = _min_ww;
			min_hh = _min_hh;
			init();
		}
		
		private function set_mc_arr(obj:Object):void
		{
			for (var i2=0 in obj.mc_2_arr) 
			{
				var mc2:Style = new Style(obj.mc_2_arr[i2]);
				mc_2_arr.push(mc2);
			}
			for (var i3=0 in obj.mc_3_arr) 
			{
				var mc3:Style = new Style(obj.mc_3_arr[i3]);
				mc_3_arr.push(mc3);
			}
			for (var i4=0 in obj.mc_4_arr) 
			{
				var mc4:Style = new Style(obj.mc_4_arr[i4]);
				mc_4_arr.push(mc4);
			}
			for (var i5=0 in obj.mc_5_arr) 
			{
				var mc5:Style = new Style(obj.mc_5_arr[i5]);
				mc_5_arr.push(mc5);
			}
			for (var i6=0 in obj.mc_6_arr) 
			{
				var mc6:Style = new Style(obj.mc_6_arr[i6]);
				mc_6_arr.push(mc6);
			}
			for (var i7=0 in obj.mc_7_arr) 
			{
				var mc7:Style = new Style(obj.mc_7_arr[i7]);
				mc_7_arr.push(mc7);
			}
			for (var i8=0 in obj.mc_8_arr) 
			{
				var mc8:Style = new Style(obj.mc_8_arr[i8]);
				mc_8_arr.push(mc8);
			}
			for (var i9=0 in obj.mc_9_arr) 
			{
				var mc9:Style = new Style(obj.mc_9_arr[i9]);
				mc_9_arr.push(mc9);
			}
		}
		private function set_bg_arr(obj:Object):void 
		{
			for (var i=0 in obj.bg_5_arr) 
			{
				var bg:Style = new Style(obj.bg_5_arr[i]);
				bg_5_arr.push(bg);
			}
			for (var i1=0 in obj.bg_1_arr) 
			{
				var bg1:Style = new Style(obj.bg_1_arr[i1]);
				bg_1_arr.push(bg1);
			}
			for (var i2=0 in obj.bg_2_arr) 
			{
				var bg2:Style = new Style(obj.bg_2_arr[i2]);
				bg_2_arr.push(bg2);
			}
			for (var i3=0 in obj.bg_3_arr) 
			{
				var bg3:Style = new Style(obj.bg_3_arr[i3]);
				bg_3_arr.push(bg3);
			}
			for (var i4=0 in obj.bg_4_arr) 
			{
				var bg4:Style = new Style(obj.bg_4_arr[i4]);
				bg_4_arr.push(bg4);
			}
			for (var i6=0 in obj.bg_6_arr) 
			{
				var bg6:Style = new Style(obj.bg_6_arr[i6]);
				bg_6_arr.push(bg6);
			}
			for (var i7=0 in obj.bg_7_arr) 
			{
				var bg7:Style = new Style(obj.bg_7_arr[i7]);
				bg_7_arr.push(bg7);
			}
			for (var i8=0 in obj.bg_8_arr) 
			{
				var bg8:Style = new Style(obj.bg_8_arr[i8]);
				bg_8_arr.push(bg8);
			}
			for (var i9=0 in obj.bg_9_arr) 
			{
				var bg9:Style = new Style(obj.bg_9_arr[i9]);
				bg_9_arr.push(bg9);
			}
		}
		private function init():void
		{
			stg.scaleMode = StageScaleMode.NO_SCALE;
			stg.align = StageAlign.TOP_LEFT;
			stg.addEventListener(Event.RESIZE, on_resize);
			on_resize();
		}
		
		private function on_resize(e:Event = null):void 
		{
			
			ww = stg.stageWidth;
			hh = stg.stageHeight;
			ExternalInterface.call("alert", ww + " " + hh);
			if (ww < min_ww)
			{
				ww = min_ww;
			}
			if (hh < min_hh)
			{
				hh = min_hh;
			}
			dispatchEvent(new Event("resize"));
			for (var i=0 in bg_5_arr) 
			{
				full_sc(bg_5_arr[i].mc);
				set_5(bg_5_arr[i]);
			}
			for (var i1=0 in bg_1_arr) 
			{
				full_sc(bg_1_arr[i1].mc);
			}
			for (var i2=0 in bg_2_arr) 
			{
				full_sc(bg_2_arr[i2].mc);
				set_2(bg_2_arr[i2]);
			}
			for (var i3=0 in bg_3_arr) 
			{
				full_sc(bg_3_arr[i3].mc);
				set_3(bg_3_arr[i3]);
			}
			for (var i4=0 in bg_4_arr) 
			{
				full_sc(bg_4_arr[i4].mc);
				set_4(bg_4_arr[i4]);
			}
			for (var i6=0 in bg_6_arr) 
			{
				full_sc(bg_6_arr[i6].mc);
				set_6(bg_6_arr[i6]);
			}
			for (var i7=0 in bg_7_arr) 
			{
				full_sc(bg_7_arr[i7].mc);
				set_7(bg_7_arr[i7]);
			}
			for (var i8=0 in bg_8_arr) 
			{
				full_sc(bg_8_arr[i8].mc);
				set_8(bg_8_arr[i8]);
			}
			for (var i9=0 in bg_9_arr) 
			{
				full_sc(bg_9_arr[i9].mc);
				set_9(bg_9_arr[i9]);
			}
			
			for (var j2=0 in mc_2_arr) 
			{
				set_2(mc_2_arr[j2]);
			}
			for (var j3=0 in mc_3_arr) 
			{
				set_3(mc_3_arr[j3]);
			}
			for (var j4=0 in mc_4_arr) 
			{
				set_4(mc_4_arr[j4]);
			}
			for (var j5=0 in mc_5_arr) 
			{
				set_5(mc_5_arr[j5]);
			}
			for (var j6=0 in mc_6_arr) 
			{
				set_6(mc_6_arr[j6]);
			}
			for (var j7=0 in mc_7_arr) 
			{
				set_7(mc_7_arr[j7]);
			}
			for (var j8=0 in mc_8_arr) 
			{
				set_8(mc_8_arr[j8]);
			}
			for (var j9=0 in mc_9_arr) 
			{
				set_9(mc_9_arr[j9]);
			}
		}
		
		private function set_5(mc:Style):void
		{
			mc.mc.x = ww / 2;
			mc.mc.y = hh / 2;
		}
		private function set_2(mc:Style):void
		{
			mc.mc.x = ww / 2;
		}
		private function set_3(mc:Style):void
		{
			mc.mc.x = ww - mc.right;
		}
		private function set_4(mc:Style):void
		{
			mc.mc.y = hh / 2;
		}
		private function set_6(mc:Style):void
		{
			mc.mc.x = ww - mc.right;
			mc.mc.y = hh / 2;
		}
		private function set_7(mc:Style):void
		{
			mc.mc.y = hh - mc.foot; 
		}
		private function set_8(mc:Style):void
		{
			mc.mc.y = hh - mc.foot; 
			mc.mc.x = ww / 2;
		}
		private function set_9(mc:Style):void
		{
			mc.mc.x = ww - mc.right;
			mc.mc.y = hh - mc.foot; 
		}
		private function full_sc(mc:DisplayObject):void 
		{
			var w_sc:Number = ww / mc.width;
			var h_sc:Number = hh / mc.height;
			if (w_sc>h_sc) 
			{
				mc.width = ww;
				mc.scaleY = mc.scaleX;
			}else {
				mc.height = hh;
				mc.scaleX = mc.scaleY;
			}
		}
		public function get width():Number 
		{
			return ww;
		}
		public function get height():Number 
		{
			return hh;
		}
	}
	
}
import flash.display.DisplayObject;
class Style
	{
		var top:Number =0;
		var foot:Number=0;
		var left:Number=0;
		var right:Number = 0;
		var mc:DisplayObject
		function Style(_mc)
		{
			mc = _mc;
			top = mc.y;
			left = mc.x;
			foot = mc.stage.stageHeight - top;
			right = mc.stage.stageWidth - left;
		}
	}